package com.achun.ximalayamvvm.view.custom

import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.achun.ximalayamvvm.BaseApplication
import com.achun.ximalayamvvm.PlayerActivity
import com.achun.ximalayamvvm.R
import com.achun.ximalayamvvm.databinding.PopupLayoutBinding
import com.achun.ximalayamvvm.utils.LogUtil
import com.achun.ximalayamvvm.utils.ToastUtil
import com.achun.ximalayamvvm.view.adapter.PopupListAdapter
import com.ximalaya.ting.android.opensdk.model.track.Track

class HimaPopupWindow()
    :PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT) {
    private lateinit var listAdapter:PopupListAdapter
    private var isSetDatas=false
    private lateinit var databind:PopupLayoutBinding
    init {
        //背景颜色，设置透明
        setBackgroundDrawable(ColorDrawable(Color.Transparent.toArgb()))
        //设置点击关闭
        isOutsideTouchable=true
        //设置布局
         databind = DataBindingUtil.inflate<PopupLayoutBinding>(
            LayoutInflater.from(BaseApplication.getAppContext()),
            R.layout.popup_layout,
            null,
            false
        )
        contentView=databind.root
        //设置窗口进出的动画
        animationStyle=R.style.pop_animation
        initView(databind)
    }

    private fun initView(view: PopupLayoutBinding) {
        view.run {
            //关闭窗口事件
            listClose.setOnClickListener{
                dismiss()
            }
            //设置recyclerView
            playerList.layoutManager=LinearLayoutManager(BaseApplication.getAppContext())
            listAdapter= PopupListAdapter()
            playerList.adapter=listAdapter
            playerList.addItemDecoration(object :RecyclerView.ItemDecoration(){
                override fun getItemOffsets(
                    outRect: Rect,
                    itemPosition: Int,
                    parent: RecyclerView
                ) {
                    outRect.top=2
                    outRect.bottom=2
                }
            })
            //列表点击事件
            listAdapter.setOnItemClickListenter(object :PopupListAdapter.OnItemClickListenter{
                override fun onClick(position: Int) {
                    onItemClickListenter.onClick(position)
                }

            })
        }
    }

    fun setDatas(tracks: List<Track>) {
            if (!isSetDatas) {
                LogUtil.e(this,"给数据---")
                listAdapter.setDatas(tracks)
                isSetDatas = true
            }
    }

    fun setPlayingIndex(position: Int) {
        listAdapter.setPlayIndex(position)
        databind.playerList.scrollToPosition(position)
    }
    interface OnItemClickListenter{
        fun onClick(position:Int)
    }
    private lateinit var onItemClickListenter: OnItemClickListenter
    fun setOnItemClickListenter(onItemClickListenter: OnItemClickListenter){
        this.onItemClickListenter=onItemClickListenter
    }

    fun setPlayMode(playMode: PlayerActivity.PlayMode) {
        when (playMode) {
            PlayerActivity.PlayMode.SORT -> {
                databind.modeIcon.setImageResource(R.drawable.selecter_sort_loop_btn)
                databind.modeTitle.text = "顺序播放"

            }
            PlayerActivity.PlayMode.RANDOM -> {
                databind.modeIcon.setImageResource(R.drawable.selecter_random_loop_btn)
                databind.modeTitle.text = "随机播放"


            }
            PlayerActivity.PlayMode.LOOP -> {
                databind.modeIcon.setImageResource(R.drawable.selecter_loop_one_btn)
                databind.modeTitle.text = "单曲循环"

            }
        }
    }
}